package cn.pupil.springframework.context.support;

import cn.pupil.springframework.beans.BeansException;
import cn.pupil.springframework.beans.factory.ConfigurableListableBeanFactory;
import cn.pupil.springframework.beans.factory.support.DefaultListableBeanFactory;

/**
 * @Description: 刷新应用上下抽象类，获取Bean工厂和加载资源
 * @Author: pupil
 * @Date: 2024/11/6 下午 12:23
 */
public abstract class AbstractRefreshableApplicationContext extends  AbstractApplicationContext{

    private DefaultListableBeanFactory beanFactory;

    @Override
    protected void refreshBeanFactory() throws BeansException {
        DefaultListableBeanFactory beanFactory = createBeanFactory();
        loadBeanDefinitions(beanFactory);
        this.beanFactory = beanFactory;
    }

    /**
     * 创建Bean工厂
     * @return
     */
    private DefaultListableBeanFactory createBeanFactory(){
        return new DefaultListableBeanFactory();
    }

    /**
     * 加载bean定义
     * @param beanFactory
     */
    protected abstract void loadBeanDefinitions(DefaultListableBeanFactory beanFactory);

    @Override
    protected ConfigurableListableBeanFactory getBeanFactory(){
        return beanFactory;
    }
}
